﻿// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.

using System;
using Byond.AudioMidi;
using Byond.Recording;
using Mvvm;
using Byond.Standalone.Messages;
using Utils.DialogManagement;
using Utils.Messaging;

namespace Byond.Standalone.ViewModels
{
	class HostControlsViewModel : ViewModelBase
	{
		private readonly IDialogManager _dialogManager;
		private readonly ViewFactory _viewFactory;
		private readonly Func<SettingsViewModel> _settingsViewModelFactory;
		private readonly Lazy<AudioRecorder> _audioRecorder;
		private readonly Lazy<IAudioInfo> _audioInfo;

		public event MessageEventHandler SendMessage;

		public bool IsPlaying
		{
			get { return _audioInfo.Value.IsSongPlaying; }
			private set
			{
				OnPropertyChanged("IsPlaying");
				OnPropertyChanged("IsPausing");
			}
		}

		public bool IsPausing { get { return !IsPlaying; } }

		private bool _isRecording;
		public bool IsRecording
		{
			get { return _audioRecorder.Value.IsWriting; }
			set
			{
				if (_audioRecorder.Value.IsWriting)
					_audioRecorder.Value.Stop();
				else
					_audioRecorder.Value.ReStart();

				OnPropertyChanged("IsRecording");
			}
		}

		private double _bpm = 120d;
		public double Bpm
		{
			get { return _bpm; }
			set
			{
				_bpm = value;
				SendMessage(new SongTempoChangeRequestMessage(_bpm));
			}
		}

		public void Play()
		{
			SendMessage(new TransportPlayRequestMessage());
		}

		public void Pause()
		{
			SendMessage(new TransportPauseRequestMessage());
		}

		public void ShowSettings()
		{
			try
			{
				var settingsViewModel = _settingsViewModelFactory();
				var dialog = _dialogManager
					.CreateCustomContentDialog(
						_viewFactory.GetViewForModel(settingsViewModel),
						"Settings",
						DialogMode.OkCancel);
				settingsViewModel.CloseRequest = dialog.Close;
				dialog.CloseBehavior = DialogCloseBehavior.ExplicitClose;
				dialog.Ok = settingsViewModel.ApplySettings;
				dialog.Cancel = dialog.Close;
				dialog.Show();
			}
			catch (Exception e)
			{
				_dialogManager
					.CreateCustomContentDialog(
						"Error showing settings: " + e.Message,
						DialogMode.Ok)
					.Show();
			}
		}

		public void HandleMessage(TransportPlayingMessage message)
		{
			IsPlaying = true;
		}

		public void HandleMessage(TransportPausingMessage message)
		{
			IsPlaying = false;
		}
	}
}
